/**
 * 
 */
package com.cats.adapter;

import java.util.ArrayList;
import com.cats.R;
import com.cats.model.ShipmentListItemModel;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * @author Jason
 *
 */
public class ShipmentListAdapter extends ArrayAdapter<ShipmentListItemModel> {

	    Context context;
	    int layoutResourceId;
	    ArrayList<ShipmentListItemModel> datas = null;

		public ShipmentListAdapter(Context context, int resourceId, ArrayList<ShipmentListItemModel> objects) {
			super(context, resourceId, objects);
	        this.layoutResourceId = resourceId;
	        this.context = context;
	        this.datas = (ArrayList<ShipmentListItemModel>) objects;
	        System.out.println("[ShipmentListAdapter]datas count:" + this.datas.size());
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View row = convertView;
	        ShipmentHolder holder = null;
	       
	        if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	           
	            holder = new ShipmentHolder();
	            holder.dateTextView = (TextView)row.findViewById(R.id.creationDate);
	            holder.trackingNumberTextView = (TextView)row.findViewById(R.id.trackingNumber);
	            holder.statusTextView = (TextView)row.findViewById(R.id.shipmentStatus);
	            holder.addressTextView = (TextView)row.findViewById(R.id.destinationAddress);
	           
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (ShipmentHolder)row.getTag();
	        }
	       
	        holder.dateTextView.setText(this.datas.get(position).getCreationDate());
	        holder.trackingNumberTextView.setText(this.datas.get(position).getTrackingNumber());
	        holder.statusTextView.setText(this.getStatusString(this.datas.get(position).getStatus()));
	        holder.addressTextView.setText(this.datas.get(position).getDestinationAddress());
	        
	        System.out.println("[MemberAdapter]getView()");
	       
	        return row;
	    }
	   
	    static class ShipmentHolder
	    {
	    	TextView dateTextView;  
	        TextView trackingNumberTextView;
	        TextView statusTextView;
	        TextView addressTextView;
	    }
	    
	    String getStatusString(int status) {
	    	if (status == 2) {
	    		return "Delivered";
	    	}
	    	else if (status == 1) {
	    		return "Transmit";
	    	}
	    	else {
	    		return "Acceptance";
	    	}
	    }
}
